package com.vietnam.power.admin.controller;

import java.util.Iterator;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.collections.CollectionUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.MessageSource;
import org.springframework.context.i18n.LocaleContextHolder;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.vietnam.power.constant.GeneralConstant;
import com.vietnam.power.domain.Product;
import com.vietnam.power.domain.ProductGroup;
import com.vietnam.power.domain.ProductGroupSearchCriteria;
import com.vietnam.power.domain.ProductSearchCriteria;
import com.vietnam.power.service.FileService;
import com.vietnam.power.service.ProductService;

@Controller(value = "adminProductController")
@RequestMapping("/admin")
public class ProductController {
	private static Logger logger = Logger.getLogger(ProductController.class);
	@Autowired
	private MessageSource messageSource;

	@Autowired
	private ProductService productService;

	@Autowired
	private FileService fileService;

	@RequestMapping(value = { "/saveProductGroup" }, method = RequestMethod.POST)
	public String saveProductGroup(HttpServletRequest request, HttpServletResponse response, @ModelAttribute(value = "productGroup") ProductGroup productGroup) {

		logger.debug(productGroup.getId());
		if (productGroup.getId() == null) {
			productService.createProductGroup(productGroup);
		} else if (productGroup.getId() != null) {
			productService.updateProductGroup(productGroup);
		}

		return "redirect:/admin/viewProductGroup.html?id=" + productGroup.getId();
	}

	@RequestMapping(value = { "/createProductGroup" }, method = RequestMethod.GET)
	public ModelAndView createProductGroup(HttpServletRequest request, HttpServletResponse response,
			@ModelAttribute(value = "productGroup") ProductGroup productGroup) {
		ModelAndView mav = new ModelAndView("admin/newProductGroup");

		mav.addObject("parentPG", productService.getProductGroupAsClientMenu());
		mav.addObject("productGroup", new ProductGroup());
		mav.addObject("pageTitle", messageSource.getMessage("admin.product.group.title", null, LocaleContextHolder.getLocale()));
		return mav;
	}

	@RequestMapping(value = { "/viewProductGroup" }, method = RequestMethod.GET)
	public ModelAndView viewProductGroup(HttpServletRequest request, HttpServletResponse response, @RequestParam(value = "id", required = true) int id) {
		ModelAndView mav = new ModelAndView("admin/newProductGroup");

		mav.addObject("parentPG", productService.getProductGroupAsClientMenu());
		mav.addObject("productGroup", productService.getProductGroupById(id));
		mav.addObject("mode", GeneralConstant.MODE_VIEW);
		mav.addObject("pageTitle", messageSource.getMessage("admin.product.group.title", null, LocaleContextHolder.getLocale()));
		return mav;
	}

	@RequestMapping(value = { "/updateProductGroup" }, method = RequestMethod.GET)
	public ModelAndView updateProductGroup(HttpServletRequest request, HttpServletResponse response, @RequestParam(value = "id", required = true) int id) {
		ModelAndView mav = new ModelAndView("admin/newProductGroup");
		logger.debug("product group id: " + id);

		List<ProductGroup> pgs = productService.getProductGroupAsClientMenu();
		
		ProductGroup pg = productService.getProductGroupById(id);
		Iterator<ProductGroup> itr = pgs.iterator();
		while (itr.hasNext()){
			ProductGroup pgi = itr.next();
			
			if (pg.getId().intValue() == pgi.getId().intValue()) {
				itr.remove();
			}
		}
		
		mav.addObject("mode", GeneralConstant.MODE_EDIT);
		mav.addObject("parentPG", productService.getProductGroupAsClientMenu());
		mav.addObject("productGroup", productService.getProductGroupById(id));
		mav.addObject("pageTitle", messageSource.getMessage("admin.product.group.title", null, LocaleContextHolder.getLocale()));
		return mav;
	}

	@RequestMapping(value = { "/searchProductGroup" }, method = RequestMethod.GET)
	public ModelAndView searchProductGroup(HttpServletRequest request, HttpServletResponse response) {
		ModelAndView mav = new ModelAndView("admin/searchProductGroup");

		mav.addObject("searchProductGroup", new ProductGroupSearchCriteria());
		mav.addObject("pageTitle", messageSource.getMessage("admin.product.group.title", null, LocaleContextHolder.getLocale()));
		return mav;
	}

	@RequestMapping(value = { "/searchProductGroup" }, method = RequestMethod.POST)
	public ModelAndView searchProductGroup(HttpServletRequest request, HttpServletResponse response,
			@ModelAttribute(value = "searchProductGroup") ProductGroupSearchCriteria s) {
		ModelAndView mav = new ModelAndView("admin/searchProductGroup");

		mav.addObject("searchProductGroup", s);
		mav.addObject("rs", productService.searchProductGroup(s));
		mav.addObject("pageTitle", messageSource.getMessage("admin.product.group.title", null, LocaleContextHolder.getLocale()));
		return mav;
	}

	@RequestMapping(value = { "/deleteProductGroup/{id}" }, method = RequestMethod.DELETE)
	public @ResponseBody
	void deleteProductGroup(HttpServletRequest request, HttpServletResponse response, @PathVariable(value = "id") int id) {
		productService.deleteProductGroup(id);
	}

	/*
	 * Product
	 */
	@RequestMapping(value = { "/saveProduct" }, method = RequestMethod.POST)
	public String saveProduct(HttpServletRequest request, HttpServletResponse response, @ModelAttribute(value = "product") Product product) {

		if (product.getId() == null) {
			productService.createProduct(product);
		} else if (product.getId() != null) {
			List<Integer> fileIds = productService.updateProduct(product);
			if (CollectionUtils.isNotEmpty(fileIds)) {
				for (Integer i : fileIds) {
					fileService.deleteFile(i, request.getSession().getServletContext().getRealPath("/"));
				}
			}
		}

		return "redirect:/admin/viewProduct.html?id=" + product.getId();
	}

	@RequestMapping(value = { "/createProduct" }, method = RequestMethod.GET)
	public ModelAndView createProduct(HttpServletRequest request, HttpServletResponse response) {
		ModelAndView mav = new ModelAndView("admin/newProduct");

		ProductGroupSearchCriteria s = new ProductGroupSearchCriteria();
		s.setStatus(Boolean.TRUE);
		mav.addObject("productGroup", productService.searchProductGroup(s));
		mav.addObject("product", new Product());
		mav.addObject("pageTitle", messageSource.getMessage("admin.product.title", null, LocaleContextHolder.getLocale()));
		return mav;
	}

	@RequestMapping(value = { "/viewProduct" }, method = RequestMethod.GET)
	public ModelAndView viewProduct(HttpServletRequest request, HttpServletResponse response, @RequestParam(value = "id", required = true) int id) {
		ModelAndView mav = new ModelAndView("admin/newProduct");

		ProductGroupSearchCriteria s = new ProductGroupSearchCriteria();
		s.setStatus(Boolean.TRUE);
		mav.addObject("productGroup", productService.searchProductGroup(s));
		mav.addObject("product", productService.getProductById(id));
		mav.addObject("mode", GeneralConstant.MODE_VIEW);
		mav.addObject("pageTitle", messageSource.getMessage("admin.product.title", null, LocaleContextHolder.getLocale()));
		return mav;
	}

	@RequestMapping(value = { "/updateProduct" }, method = RequestMethod.GET)
	public ModelAndView updateProduct(HttpServletRequest request, HttpServletResponse response, @RequestParam(value = "id", required = true) int id) {
		ModelAndView mav = new ModelAndView("admin/newProduct");

		ProductGroupSearchCriteria s = new ProductGroupSearchCriteria();
		s.setStatus(Boolean.TRUE);
		mav.addObject("productGroup", productService.searchProductGroup(s));
		mav.addObject("product", productService.getProductById(id));
		mav.addObject("mode", GeneralConstant.MODE_EDIT);
		mav.addObject("pageTitle", messageSource.getMessage("admin.product.title", null, LocaleContextHolder.getLocale()));
		return mav;
	}

	@RequestMapping(value = { "/searchProduct" }, method = RequestMethod.GET)
	public ModelAndView searchProduct(HttpServletRequest request, HttpServletResponse response) {
		ModelAndView mav = new ModelAndView("admin/searchProduct");

		ProductGroupSearchCriteria s = new ProductGroupSearchCriteria();
		s.setStatus(Boolean.TRUE);
		mav.addObject("productGroup", productService.searchProductGroup(s));
		mav.addObject("searchProduct", new ProductSearchCriteria());
		mav.addObject("pageTitle", messageSource.getMessage("admin.product.title", null, LocaleContextHolder.getLocale()));
		return mav;
	}

	@RequestMapping(value = { "/searchProduct" }, method = RequestMethod.POST)
	public ModelAndView searchProduct(HttpServletRequest request, HttpServletResponse response,
			@ModelAttribute(value = "searchProduct") ProductSearchCriteria s) {
		ModelAndView mav = new ModelAndView("admin/searchProduct");

		ProductGroupSearchCriteria sg = new ProductGroupSearchCriteria();
		sg.setStatus(Boolean.TRUE);
		mav.addObject("productGroup", productService.searchProductGroup(sg));
		mav.addObject("searchProduct", s);
		mav.addObject("rs", productService.searchProduct(s));
		mav.addObject("pageTitle", messageSource.getMessage("admin.product.title", null, LocaleContextHolder.getLocale()));
		return mav;
	}

	@RequestMapping(value = { "/deleteProduct/{id}" }, method = RequestMethod.DELETE)
	public @ResponseBody
	void deleteProduct(HttpServletRequest request, HttpServletResponse response, @PathVariable(value = "id") int id) {
		List<Integer> deletedFileIds = productService.deleteProduct(id);
		for (Integer i : deletedFileIds) {
			fileService.deleteFile(i, request.getSession().getServletContext().getRealPath("/"));
		}
	}
}
